// pages/search/index.js
import {request} from "../../request/index.js";
import regeneratorRuntime, { async } from "../../libs/runtime/runtime";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods:[],
    isFocus:false,
    inputValue:''
  },
  TimeId:-1,
  //输入框的改变就会触发的事件
  handleInput(e){
    //获取输入框的值
    const {value}=e.detail;
    //检验合法性
    if(!value.trim()){
      //值不合法
      this.setData({
        goods:[],
        isFocus:false
      })
      return;
    }
    //准备发送请求获取数据
    this.setData({
      isFocus:true
    })
    clearTimeout(this.TimeId);
    this.TimeId=setTimeout(()=>{
      this.qsearch(value);
    },1000)
    
  },
  //发送请求获取搜索的数据
  async qsearch(query){
    const res=await request({url:"/goods/qsearch",data:{query}});
    this.setData({
      goods:res
    })
  },
  //点击取消按钮
  handleCancel(){
    this.setData({
      inputValue:'',
      isFocus:false,
      goods:[]
    })
  }
})